home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / xcontact / lib / OkStr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.5 KB  |  247 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #pragma    once
  18.  
  19. #include "stdlib.h"
  20. #include "OkTypes.h"
  21. #include "stream.h"
  22.  
  23. // Temporary strings are generated by the concatenation operators.
  24. // They are designed to avoid calls to malloc, and as such are not
  25. // meant to be used directly.
  26.  
  27. class OkStr;
  28.  
  29. class OkTempStr {
  30. public:
  31.     OkTempStr(OkTempStr const &other);
  32.     ~OkTempStr();
  33.  
  34.     void destroy();
  35.  
  36.     // C++ uses these operators to perform the first concatenation
  37.     friend OkTempStr operator|(OkStr const&, OkStr const&);
  38.     friend OkTempStr operator|(OkStr const&, char const*);
  39.     friend OkTempStr operator|(char const*, OkStr const&);
  40.  
  41.     // Susequent concatenations use these operators.  These operators
  42.     // just append the argument data to the temporary, avoiding malloc
  43.     // when possible.
  44.     // XXX these aren't really const, although they are declared as such
  45.     // XXX to avoid warnings. In a land of very advanced compilers, this
  46.     // XXX strategy may backfire.
  47.     friend OkTempStr& operator|(const OkTempStr&, OkStr const& b);
  48.     friend OkTempStr& operator|(const OkTempStr&, char const* b);
  49.     friend ostream& operator<<(ostream& os, const OkTempStr& a)
  50.                   { os << (char*) a; return os; };
  51.  
  52.     operator char*() const
  53.     { return data; }
  54.     operator int() const
  55.     { return atoi(data); }
  56.     operator float() const
  57.     { return float(atof(data)); }
  58.     operator double() const
  59.     { return double(atof(data)); }
  60.  
  61.     u_int length() const
  62.     { return slength - 1; }
  63.  
  64. protected:
  65.     char    indata[100];        // inline data, avoiding malloc
  66.     char*    data;            // points at indata or heap
  67.     u_int    slength;        // same rules as OkStr::slength
  68.  
  69.     OkTempStr(char const *, u_int, char const *, u_int);
  70.  
  71.     OkTempStr& concat(char const* b, u_int bl);
  72.  
  73.     friend class OkStr;
  74. };
  75.  
  76. //----------------------------------------------------------------------
  77.  
  78. class OkStr {
  79.     friend class OkTempStr;
  80. public:
  81.     OkStr(u_int l=0);
  82.     OkStr(char const *s);
  83.     OkStr(char const *s, u_int len);
  84.     OkStr(OkStr const&);
  85.     OkStr(char *s, char const* format);
  86.     OkStr(int, char const* format);
  87.     OkStr(float, char const* format);
  88.     OkStr(double, char const* format);
  89.     OkStr(const OkTempStr&);
  90.  
  91.     ~OkStr();
  92.     void destroy();
  93.  
  94.     u_long hash() const;
  95.  
  96.     operator char*() const
  97.     { return data; }
  98.     operator int() const
  99.     { return atoi(data); }
  100.     operator float() const
  101.     { return float(atof(data)); }
  102.     operator double() const
  103.     { return double(atof(data)); }
  104.  
  105.     u_int length() const { return slength-1; }
  106.  
  107.     char& operator[](u_int i) const
  108.     {   assert(i<slength-1);
  109.     return data[i]; }
  110.  
  111.     void operator=(const OkTempStr& s);
  112.     void operator=(OkStr const& s);
  113.     void operator=(char const *s);
  114.  
  115.     friend ostream& operator<<(ostream& os, const OkStr& a)
  116.                   { os << (char*) a; return os; };
  117.     
  118.     /////////////////////////////////////////////////////
  119.     // Comparison
  120.     friend OkBool operator==(OkStr const&, OkStr const&);
  121.     friend OkBool operator==(OkStr const&, char const*);
  122.     friend OkBool operator==(char const*, OkStr const&);
  123.  
  124.     friend OkBool operator!=(OkStr const&, OkStr const&);
  125.     friend OkBool operator!=(OkStr const&, char const*);
  126.     friend OkBool operator!=(char const*, OkStr const&);
  127.  
  128.     friend OkBool operator>=(OkStr const&, OkStr const&);
  129.     friend OkBool operator>=(OkStr const&, char const*);
  130.     friend OkBool operator>=(char const*, OkStr const&);
  131.  
  132.     friend OkBool operator<=(OkStr const&, OkStr const&);
  133.     friend OkBool operator<=(OkStr const&, char const*);
  134.     friend OkBool operator<=(char const*, OkStr const&);
  135.  
  136.     friend OkBool operator>(OkStr const&, OkStr const&);
  137.     friend OkBool operator>(OkStr const&, char const*);
  138.     friend OkBool operator>(char const*, OkStr const&);
  139.  
  140.     friend OkBool operator<(OkStr const&, OkStr const&);
  141.     friend OkBool operator<(OkStr const&, char const*);
  142.     friend OkBool operator<(char const*, OkStr const&);
  143.  
  144.     int compare(OkStr const *a) const { return ::compare(*this, *a); }
  145.     friend int compare(OkStr const&, OkStr const&);
  146.     friend int compare(OkStr const&, char const*);
  147.     friend int compare(char const*, OkStr const&);
  148.  
  149.     /////////////////////////////////////////////////////
  150.     // Concatenation
  151.     friend OkTempStr& operator|(const OkTempStr&, OkStr const&);
  152.     friend OkTempStr& operator|(const OkTempStr&, char const*);
  153.  
  154.     friend OkTempStr operator|(OkStr const&, OkStr const&);
  155.     friend OkTempStr operator|(OkStr const&, char const*);
  156.     friend OkTempStr operator|(char const*, OkStr const&);
  157.  
  158.     /////////////////////////////////////////////////////
  159.     // Misc
  160.     OkStr copy() const;
  161.     OkStr extract(u_int start,u_int len) const;
  162.     OkStr cut(u_int start,u_int len);
  163.     OkStr head(u_int);
  164.     OkStr tail(u_int);
  165.     void lower();
  166.     void raise();
  167.  
  168.     u_int cnt(char a);
  169.  
  170.     void remove(u_int posn,u_int len=1);
  171.  
  172.     void resize(u_int len, OkBool reallocate = FALSE);
  173.     void setMaxLength(u_int maxlen);
  174.  
  175.     void append(char a);
  176.     void append(char const *s, u_int len=0);
  177.     void append(const OkTempStr& s)
  178.     { append((char*)s, s.slength-1); }
  179.     void append(OkStr const& s)
  180.     { append((char*)s, s.slength-1); }
  181.  
  182.     void insert(char a, u_int posn=0);
  183.     void insert(char const *, u_int posn=0, u_int len=0);
  184.     void insert(const OkTempStr& s, u_int posn=0)
  185.     { insert((char*)s, posn, s.slength-1); }
  186.     void insert(OkStr const& s, u_int posn=0)
  187.     { insert((char*)s, posn, s.slength-1); }
  188.  
  189.     /////////////////////////////////////////////////////
  190.     // Parsing
  191.     u_int next(u_int posn, char delimiter) const;
  192.     u_int next(u_int posn, char const *delimiters, u_int len=0) const;
  193.     u_int next(u_int posn, OkStr const& delimiters) const
  194.     { return next(posn, (char*)delimiters, delimiters.slength-1); }
  195.                 
  196.     u_int nextR(u_int posn, char delimiter) const;
  197.     u_int nextR(u_int posn, char const*, u_int len=0) const;
  198.     u_int nextR(u_int posn, OkStr const& delimiters) const
  199.     { return nextR(posn, (char*)delimiters, delimiters.slength-1); }
  200.                       
  201.     u_int skip(u_int posn, char a) const; 
  202.     u_int skip(u_int posn, char const *, u_int len=0) const;
  203.     u_int skip(u_int posn, OkStr const& delimiters) const
  204.     { return skip(posn, (char*)delimiters, delimiters.slength-1); }
  205.  
  206.     u_int skipR(u_int posn, char a) const;
  207.     u_int skipR(u_int posn, char const *, u_int len=0) const;
  208.     u_int skipR(u_int posn, OkStr const& delimiters) const
  209.     { return skipR(posn, (char*)delimiters, delimiters.slength-1); }
  210.  
  211.     OkStr token(u_int & posn, char delimiter) const;
  212.     OkStr token(u_int & posn, char const * delimiters,
  213.     u_int delimiters_len = 0) const;
  214.     OkStr token(u_int & posn, OkStr const & delimiters) const
  215.     { return token(posn, delimiters.data, delimiters.slength-1); }
  216.     // same as token() except it returns a null token when
  217.     // there are two delimiters adjacent to each other
  218.     OkStr ptoken(u_int & posn, char delimiter) const;
  219.  
  220.     OkStr tokenR(u_int & posn, char delimiter) const;
  221.     OkStr tokenR(u_int & posn, char const * delimiters,
  222.     u_int delimiters_len = 0) const;
  223.     OkStr tokenR(u_int & posn, OkStr const & delimiters) const
  224.     { return tokenR(posn, delimiters.data, delimiters.slength-1); }
  225.  
  226. protected:
  227.     // slength is one greater than the true length of the data.
  228.     // This is because the data is null-terminated. However, the
  229.     // data may contain nulls; they will be ignored. This is to
  230.     // provide compatibility with ordinary C-style strings, and
  231.     // with arbitrary data. slength is always positive.
  232.     u_int slength;
  233.  
  234.     // data points to the actual data. It is always a valid pointer.
  235.     char * data; 
  236.  
  237.     // zero-length string support
  238.     // resizeInternal doesn't update slength or handle null termination
  239.     static char OkStr::emptyString;
  240.     void OkStr::resizeInternal(u_int);
  241.  
  242.     int findEndBuffer(const char *, u_int buflen) const;
  243.     int findBuffer(const char *buf, u_int buflen) const;
  244.     void bracketBuffer(const char *, u_int buflen, int &, int &) const;
  245. };
  246.  
  247.